<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>react-redux的学习 | 刘超的博客</title><meta name="author" content="刘超"><meta name="copyright" content="刘超"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="公共状态&emsp;状态管理是单页面应用解决组件状态共享，复杂组件通信的技术方案 状态管理的应用场景&emsp;状态管理为什么这么受欢迎,首先想想状态管理适用于什么场景,解决了什么问题。  全局状态管理,针对整个系统做数据管理。 组件之间共用数据 复杂组件之间的通信  React-Redux,Redux 和 React 三者关系&emsp;在深入研究 React-Redux 之前,首先要明白 Re">
<meta property="og:type" content="article">
<meta property="og:title" content="react-redux的学习">
<meta property="og:url" content="https://leachliu.gitee.io/2022/01/26/react/react-redux%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="刘超的博客">
<meta property="og:description" content="公共状态&emsp;状态管理是单页面应用解决组件状态共享，复杂组件通信的技术方案 状态管理的应用场景&emsp;状态管理为什么这么受欢迎,首先想想状态管理适用于什么场景,解决了什么问题。  全局状态管理,针对整个系统做数据管理。 组件之间共用数据 复杂组件之间的通信  React-Redux,Redux 和 React 三者关系&emsp;在深入研究 React-Redux 之前,首先要明白 Re">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://leachliu.gitee.io/images/avatar.webp">
<meta property="article:published_time" content="2022-01-25T16:00:00.000Z">
<meta property="article:modified_time" content="2023-11-03T07:37:06.444Z">
<meta property="article:author" content="刘超">
<meta property="article:tag" content="redux">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://leachliu.gitee.io/images/avatar.webp"><link rel="shortcut icon" href="/images/favicon.ico"><link rel="canonical" href="https://leachliu.gitee.io/2022/01/26/react/react-redux%E5%AD%A6%E4%B9%A0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: {"appId":"VFZO06XK8A","apiKey":"1202cec78a85d208874b3c9cbcd0e212","indexName":"index total","hits":{"per_page":10},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'react-redux的学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-03 15:37:06'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><script defer src="/js/custom.js"></script><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"><link rel="stylesheet" href="/css/universe.css"><link rel="stylesheet" href="/css/custom.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/images/avatar.webp" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">41</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="刘超的博客"><span class="site-name">刘超的博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">react-redux的学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-25T16:00:00.000Z" title="发表于 2022-01-26 00:00:00">2022-01-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-03T07:37:06.444Z" title="更新于 2023-11-03 15:37:06">2023-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/react/">react</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="react-redux的学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h2 id="公共状态"><a href="#公共状态" class="headerlink" title="公共状态"></a>公共状态</h2><p>&emsp;状态管理是单页面应用解决组件状态共享，复杂组件通信的技术方案</p>
<h3 id="状态管理的应用场景"><a href="#状态管理的应用场景" class="headerlink" title="状态管理的应用场景"></a>状态管理的应用场景</h3><p>&emsp;状态管理为什么这么受欢迎,首先想想状态管理适用于什么场景,解决了什么问题。</p>
<ul>
<li>全局状态管理,针对整个系统做数据管理。</li>
<li>组件之间共用数据</li>
<li>复杂组件之间的通信</li>
</ul>
<h3 id="React-Redux-Redux-和-React-三者关系"><a href="#React-Redux-Redux-和-React-三者关系" class="headerlink" title="React-Redux,Redux 和 React 三者关系"></a>React-Redux,Redux 和 React 三者关系</h3><p>&emsp;在深入研究 React-Redux 之前,首先要明白 React-Redux,Redux 和 React 三者的关系</p>
<ul>
<li><p><font color="red">Redux:</font> 首先 Redux 是一个状态管理 js 库,本身并没有和 React 有什么关系.换句话说 Redux 可以应用与其他的前端框架,甚至也可以应用于 Vue 中。</br></p>
</li>
<li><p><font color="red">React-Redux:</font> React-Redux 是连接 React 应用和 Redux 状态管理的桥梁。React-Redux 主要专注做两件事情,一是如何向 React 应用中注入 Redux 中的 store,二是如何根据 Store 的改变,把消息发放到应用中每一个需要状态的组件。</br></p>
</li>
<li><p><font color="red">React:</font> 应用就不必多说了。</p>
</li>
<li><p>三者的关系图如下</p>
</li>
</ul>
<p><img src=""></p>
<h3 id="温习-Redux"><a href="#温习-Redux" class="headerlink" title="温习 Redux"></a>温习 Redux</h3><p>&emsp;Redux 的设计满足以下<font color="black" fontWeight="bold">三个原则</font></p>
<ul>
<li>1 单向数据流: 整个 Redux 数据流都是单向的<br><img src=""></li>
<li>2 state:在 Redux 中不能通过直接改变 state ,来让状态发生变化,如果想要改变 state,那就必须触发一次 action,通过 action 执行每个 reducer</li>
<li>3 纯函数执行: 每一个 reducer 都是一个纯函数,里面不要执行任何副作用,返回的值作为新的 state,state 改变会触发 store 里面的 subscribe</li>
</ul>
<h3 id="发布订阅思想"><a href="#发布订阅思想" class="headerlink" title="发布订阅思想"></a>发布订阅思想</h3><p>&emsp;redux 可以作为<font color="black" size="4">发布订阅模式</font>的一个具体实现。redux 都会创建一个 store,里面保存了状态信息,改变 store 的方法 dispatch,以及订阅变化的 subscribe</p>
<h3 id="react-redux"><a href="#react-redux" class="headerlink" title="react-redux"></a>react-redux</h3><ul>
<li><p>目录</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">*---store   <span class="comment">// 存放redux，数据，以及action</span></span><br><span class="line">*---store子目录</span><br><span class="line">  actions  <span class="comment">// 存放action的文件夹 actionType(模板中是放在一起 如果在项目中可以单独新建一个文件存放type)</span></span><br><span class="line">  reducers <span class="comment">// 存放reducer的文件夹</span></span><br><span class="line">  states <span class="comment">// 存放state的文件夹</span></span><br><span class="line">  index.<span class="property">js</span> <span class="comment">// store的入口文件</span></span><br></pre></td></tr></table></figure></li>
<li><p>安装</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">yarn add react react-redux</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用</p>
<p>先在 index.js 中引入 react-redux 定义的 Provider 组件，并包裹在 App 组件外部</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDOM</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; provider &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">&quot;./App&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>接着在 store 目录下的 index.js 中创建一个 store,并抛出</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createStore, combineReducers, applyMiddleware &#125; <span class="keyword">from</span> <span class="string">&quot;redux&quot;</span>;</span><br><span class="line"><span class="comment">//createStore是用来创建store的,combineReducers是用来合并多个reducer的</span></span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&quot;redux-thunk&quot;</span>;</span><br><span class="line"><span class="comment">//创建根reducer,用combineReducers合并多个reducers,此处还未定义reducer,所以暂空</span></span><br><span class="line"><span class="keyword">const</span> reducers = <span class="title function_">combineReducers</span>(&#123;&#125;);</span><br></pre></td></tr></table></figure>

<p>在 src&#x2F;index.js 中引入 store，并且在 Provider 组件上使用</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* 之前的代码 */</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&quot;store&quot;</span></span><br><span class="line"><span class="title class_">ReactDom</span>.<span class="title function_">render</span>(</span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span>=<span class="string">&#123;store&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;<span class="name">App</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span>,</span></span><br><span class="line"><span class="language-xml">  documnet.getElementById(&quot;root&quot;)</span></span><br><span class="line"><span class="language-xml">)</span></span><br></pre></td></tr></table></figure>

<p>接着可以定义一个数据文件夹,state 文件夹,在 src&#x2F;store&#x2F;state 目录下,新建例如 globalUserInfo.ts</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">interface</span> globalUserinfoStateInterface &#123;</span><br><span class="line">  <span class="attr">premissions</span>: <span class="built_in">any</span>;</span><br><span class="line">  <span class="attr">token</span>: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="attr">globalUserinfoState</span>: globalUserinfoStateInterface = &#123;</span><br><span class="line">  <span class="attr">premissions</span>: [],</span><br><span class="line">  <span class="attr">token</span>: <span class="string">&quot;222223&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> globalUserinfoState;</span><br></pre></td></tr></table></figure>

<p>然后定义 reducer,新建 reducer 文件夹,新建例如 globalUserReducer.ts</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> initiaState <span class="keyword">from</span> <span class="string">&quot;../states/globalUserInfo&quot;</span>;</span><br><span class="line"><span class="comment">// 定义reducer，第一个参数为state，赋予默认值为上边引入的的initiaState，</span></span><br><span class="line"><span class="comment">// 第二个参数为action，并return一个state</span></span><br><span class="line"><span class="comment">// 并且抛出这个countReducer</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">countReducer</span>(<span class="params">state = initiaState, action</span>) &#123;</span><br><span class="line">  <span class="comment">//这里是纯函数 根据dispatch执行的type来执行逻辑 更新state</span></span><br><span class="line">  <span class="keyword">switch</span> (action.<span class="property">type</span>) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;CHANGE_USERINFO_PREMISSIONS&quot;</span>:</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        <span class="attr">premissions</span>: action.<span class="property">payload</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;CHANGE_USERINFO_TOKEN&quot;</span>:</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        <span class="attr">token</span>: action.<span class="property">payload</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;DELETE_USERINFO_TOKEN&quot;</span>:</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        <span class="attr">token</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">    <span class="attr">default</span>:</span><br><span class="line">      <span class="keyword">return</span> state;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;修改数据首先需要定义一个 dispatch，在 redux 中，修改数据必须通过 dispatch 提交一个 action 来进行，新建一个文件夹 src&#x2F;store&#x2F;actions&#x2F;globalUserInfo.ts<br>&emsp;在 globalUserInfo.ts 中定义 changeUserPermission 方法，并 return 一个对象，对象上有一个 type 属性，这个属性对应的是一个常量字符串,在 reducers 中也有一个和它相同匹配的 type 来进行逻辑执行<br>&emsp;当然是函数就可以传参,这个 payload 就是传的参数,action 中定义的对象的属性是 payload，所以在 reducer 中进行参数使用的时候，也是使用 action.payload</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="variable constant_">CHANGE_USERINFO_TOKEN</span> = <span class="string">&quot;CHANGE_USERINFO_TOKEN&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="variable constant_">CHANGE_USERINFO_PREMISSIONS</span> = <span class="string">&quot;CHANGE_USERINFO_PREMISSIONS&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="variable constant_">DELETE_USERINFO_TOKEN</span> = <span class="string">&quot;DELETE_USERINFO_TOKEN&quot;</span>;</span><br><span class="line"><span class="comment">//同步方式获取token</span></span><br><span class="line"><span class="keyword">function</span> changeUserToken&lt;T&gt;(<span class="attr">payload</span>: T) &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">type</span>: <span class="variable constant_">CHANGE_USERINFO_TOKEN</span>,</span><br><span class="line">    payload,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//异步处理方式获取权限</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">changeUserPermission</span>(<span class="params">payload: any</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">async</span> (dispatch) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">          <span class="attr">type</span>: <span class="variable constant_">CHANGE_USERINFO_PREMISSIONS</span>,</span><br><span class="line">          payload,</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="title function_">resolve</span>(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">      &#125;, <span class="number">1000</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 同步方式-删除token</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">deleteUserToken</span>(<span class="params">payload</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">type</span>: <span class="variable constant_">DELETE_USERINFO_TOKEN</span>,</span><br><span class="line">    payload,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> &#123; changeUserToken, changeUserPermission, deleteUserToken &#125;;</span><br></pre></td></tr></table></figure>

<p>在 src&#x2F;store&#x2F;index.ts 中引入定义的 globalUserReducer,合并到 rootReducer 中,并且配置 redux-thunk 中间件增强 dispatch 功能,异步调用。</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createStore, combineReducers, applyMiddleware &#125; <span class="keyword">from</span> <span class="string">&quot;redux&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&quot;redux-thunk&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> globalUserInfoState, &#123;</span><br><span class="line">  globalUserinfoStateInterface,</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;./states/globalUserInfo&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> globalUserinfoReducer <span class="keyword">from</span> <span class="string">&quot;./reducers/globalUserReducer&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">interface</span> allStatesInterface &#123;</span><br><span class="line">  <span class="attr">globalUserinfo</span>: globalUserinfoStateInterface;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//合并所有 redux</span></span><br><span class="line"><span class="keyword">const</span> allReducer = <span class="title function_">combineReducers</span>(&#123;</span><br><span class="line">  <span class="attr">globalUserInfo</span>: globalUserinfoReducer,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//合并所有 state</span></span><br><span class="line"><span class="keyword">const</span> allStates = &#123;</span><br><span class="line">  <span class="attr">globalUserInfo</span>: globalUserInfoState,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//createStore三个参数</span></span><br><span class="line"><span class="comment">//参数一 reducers ： redux 的 reducer ，如果有多个那么可以调用 combineReducers 合并。</span></span><br><span class="line"><span class="comment">//参数二 initialState ：初始化的 state 。</span></span><br><span class="line"><span class="comment">//参数三 middleware ：如果有中间件，那么存放 redux 中间件。</span></span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">createStore</span>(</span><br><span class="line">  <span class="comment">//所有事件</span></span><br><span class="line">  allReducer,</span><br><span class="line">  <span class="comment">//所有数据</span></span><br><span class="line">  allStates,</span><br><span class="line">  <span class="title function_">applyMiddleware</span>(thunk)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<p>接着只需要改造一下组件就可以使用了,就可以正常使用了</p>
</li>
<li><p>connect<br>&emsp;既然已经全局注入了 Store ，那么需要 Store 中的状态或者想要改变 Store 的状态，那么如何处理呢，React-Redux 提供了一个高阶组件 connect，被 connect 包装后组件将获得如下功能：</p>
<ul>
<li>能够从 props 中获取改变 state 的方法 Store.dispatch 。</li>
<li>如果 connect 有第一个参数，那么会将 redux state 中的数据，映射到当前组件的 props 中，子组件可以使用消费</li>
<li>当需要的 state ，有变化的时候，会通知当前组件更新，重新渲染视图。<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="comment">//定义获取数据的方法 mapStateToProps</span></span><br><span class="line"><span class="comment">//定义方法mapStateToProps,参数为state,返回一个对象,对象内定义需要获取store内的数据</span></span><br><span class="line"><span class="comment">//由于是使用的 globalUserInfoState的数据,但又使用了重新命名,所以使用state.globalUserInfo</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapStateToProps</span> = (<span class="params">state</span>) =&gt; (&#123; <span class="attr">token</span>: state.<span class="property">globalUserInfo</span>.<span class="property">token</span> &#125;);</span><br><span class="line"><span class="comment">//定义改变数据的方法 mapDispatchToProps</span></span><br><span class="line"><span class="comment">// 将 redux 中的 dispatch 方法，映射到业务组件的 props 中。比如将如上 demo 中的两个方法映射到 props ，变成了</span></span><br><span class="line"><span class="comment">// changeUserPermission,changeUserToken 方法</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapDispatchToProps</span> = (<span class="params">dispatch</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">changeUserPermission</span>: <span class="function">(<span class="params">payload</span>) =&gt;</span></span><br><span class="line">    <span class="title function_">dispatch</span>(<span class="title function_">changeUserPermission</span>(payload)),</span><br><span class="line">    <span class="attr">changeUserToken</span>: <span class="function">() =&gt;</span> <span class="title function_">dispatch</span>(<span class="title function_">changeUserToken</span>()),</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
调用 connect 方法改造当前组件<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">connect</span>(mapStateToProps, mapActionToProps)(<span class="title class_">App</span>);</span><br></pre></td></tr></table></figure>
完整代码<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">App</span> = (<span class="params">props</span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">/**/</span></span><br><span class="line">  <span class="keyword">const</span> &#123; token, changeUserPermission, changeUserToken &#125; = props;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapStateToProps</span> = (<span class="params">state</span>) =&gt; (&#123; <span class="attr">token</span>: state.<span class="property">globalUserInfo</span>.<span class="property">token</span> &#125;);</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">mapDispatchToProps</span> = (<span class="params">dispatch</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">changeUserPermission</span>: <span class="function">(<span class="params">payload</span>) =&gt;</span></span><br><span class="line">      <span class="title function_">dispatch</span>(<span class="title function_">changeUserPermission</span>(payload)),</span><br><span class="line">    <span class="attr">changeUserToken</span>: <span class="function">() =&gt;</span> <span class="title function_">dispatch</span>(<span class="title function_">changeUserToken</span>()),</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">connect</span>(mapStateToProps, mapDispatchToProps)(<span class="title class_">App</span>);</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>useSelector 和 useDispatch<br>react-redux 7.1 之后也可以使用 useSelector、useDispatch 等 HooksApi 替代 connect，减少模板代码。</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"> <span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">&quot;redux&quot;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; <span class="title class_">Provider</span>, useSelector, useDispatch &#125; <span class="keyword">from</span> <span class="string">&quot;react-redux&quot;</span>;</span><br><span class="line"> <span class="keyword">const</span> initialState = &#123; <span class="attr">num</span>: <span class="number">0</span> &#125;;</span><br><span class="line"> <span class="keyword">const</span> <span class="title function_">reducer</span> = (<span class="params">state, action</span>) =&gt; &#123;</span><br><span class="line">   <span class="keyword">switch</span> (action.<span class="property">type</span>) &#123;</span><br><span class="line">     <span class="keyword">case</span> <span class="string">&quot;decrement&quot;</span>:</span><br><span class="line">       <span class="keyword">return</span> &#123; ...state, <span class="attr">num</span>: state.<span class="property">num</span> - <span class="number">1</span> &#125;;</span><br><span class="line">     <span class="keyword">case</span> <span class="string">&quot;increment&quot;</span>:</span><br><span class="line">       <span class="keyword">return</span> &#123; ...state, <span class="attr">num</span>: state.<span class="property">num</span> + <span class="number">1</span> &#125;;</span><br><span class="line">     <span class="attr">default</span>:</span><br><span class="line"></span><br><span class="line">       <span class="keyword">return</span> state;</span><br><span class="line">   &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">createStore</span>(reducer, initialState);</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">ComponentUseReactRedux</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>ComponentUseReactRedux<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span>=<span class="string">&#123;store&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">ChildComponentUseReactRedux</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span></span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">ChildComponentUseReactRedux</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> num = <span class="title function_">useSelector</span>(<span class="function"><span class="params">state</span> =&gt;</span> state.<span class="property">num</span>);</span><br><span class="line">    <span class="keyword">const</span> dispatch = <span class="title function_">useDispatch</span>();</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">h3</span>&gt;</span>Using useSelector, useDispatch<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          Number: &#123;num&#125;</span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> dispatch(&#123; type: &quot;increment&quot; &#125;)&#125;&gt;+<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> dispatch(&#123; type: &quot;decrement&quot; &#125;)&#125;&gt;-<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">ComponentUseReactRedux</span>;</span><br></pre></td></tr></table></figure>

<p>通过 useSelector、useDispatch 等方法，我们可以用 Hooks 方法完成 Redux 的功能，Hooks 和 Redux 之间没有替代关系，应该更好地共存和融合。</p>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://leachliu.gitee.io">刘超</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://leachliu.gitee.io/2022/01/26/react/react-redux%E5%AD%A6%E4%B9%A0/">https://leachliu.gitee.io/2022/01/26/react/react-redux%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://leachliu.gitee.io" target="_blank">刘超的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/redux/">redux</a></div><div class="post_share"><div class="social-share" data-image="/images/avatar.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/01/27/react/react-redux%E5%85%AC%E5%85%B1%E6%95%B0%E6%8D%AE%E7%AE%A1%E7%90%86/" title="react-redux公共数据管理(React项目搭建10)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">react-redux公共数据管理(React项目搭建10)</div></div></a></div><div class="next-post pull-right"><a href="/2022/01/25/react/%E4%BE%A7%E8%BE%B9%E6%A0%8Fslider(3)/" title="侧边栏slider3(React项目搭建9)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">侧边栏slider3(React项目搭建9)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/01/27/react/react-redux%E5%85%AC%E5%85%B1%E6%95%B0%E6%8D%AE%E7%AE%A1%E7%90%86/" title="react-redux公共数据管理(React项目搭建10)"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-27</div><div class="title">react-redux公共数据管理(React项目搭建10)</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/images/avatar.webp" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">刘超</div><div class="author-info__description">只有自己强大，才不会被别人践踏。</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">41</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=923946406&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="https://chao-picture-bed.oss-cn-hangzhou.aliyuncs.com/blog/vue/popperJs/202301131417748.jpg" target="_blank" title="Wechat"><i class="fab fa-weixin"></i></a><a class="social-icon" href="/923946406@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://github.com/tzy13755126023" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="Rss"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">书山有路勤为径,学海无涯苦作舟</div></div><div class="card-widget tzy-right-widget" id="card-wechat"><div id="flip-wrapper"><div id="flip-content"><div class="face"></div><div class="back face"></div></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E7%8A%B6%E6%80%81"><span class="toc-number">1.</span> <span class="toc-text">公共状态</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">1.1.</span> <span class="toc-text">状态管理的应用场景</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#React-Redux-Redux-%E5%92%8C-React-%E4%B8%89%E8%80%85%E5%85%B3%E7%B3%BB"><span class="toc-number">1.2.</span> <span class="toc-text">React-Redux,Redux 和 React 三者关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%A9%E4%B9%A0-Redux"><span class="toc-number">1.3.</span> <span class="toc-text">温习 Redux</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E6%80%9D%E6%83%B3"><span class="toc-number">1.4.</span> <span class="toc-text">发布订阅思想</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#react-redux"><span class="toc-number">1.5.</span> <span class="toc-text">react-redux</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/11/06/comlibary/4.%E7%BB%84%E4%BB%B6%E5%BA%93%E7%B1%BB%E5%90%8D%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E4%BB%A5%E5%8F%8A%E8%A7%84/" title="无题">无题</a><time datetime="2023-11-06T07:16:00.298Z" title="发表于 2023-11-06 15:16:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/06/comlibary/3.%E6%90%AD%E5%BB%BA%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/" title="组件库框架 - 3 搭建组件库开发环境"><img src="/images/beijing4.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 3 搭建组件库开发环境"/></a><div class="content"><a class="title" href="/2023/11/06/comlibary/3.%E6%90%AD%E5%BB%BA%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/" title="组件库框架 - 3 搭建组件库开发环境">组件库框架 - 3 搭建组件库开发环境</a><time datetime="2023-11-06T06:30:00.000Z" title="发表于 2023-11-06 14:30:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/06/comlibary/2.%E5%88%9D%E5%A7%8B%E5%8C%96workspace-root/" title="组件库框架 - 2 初始化workspace-root"><img src="/images/beijing4.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 2 初始化workspace-root"/></a><div class="content"><a class="title" href="/2023/11/06/comlibary/2.%E5%88%9D%E5%A7%8B%E5%8C%96workspace-root/" title="组件库框架 - 2 初始化workspace-root">组件库框架 - 2 初始化workspace-root</a><time datetime="2023-11-06T03:18:00.000Z" title="发表于 2023-11-06 11:18:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/10/27/comlibary/1.%E6%90%AD%E5%BB%BApnpm%E3%80%81monorepo/" title="组件库框架 - 1 搭建pnpn monorepo"><img src="/images/beijing3.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 1 搭建pnpn monorepo"/></a><div class="content"><a class="title" href="/2023/10/27/comlibary/1.%E6%90%AD%E5%BB%BApnpm%E3%80%81monorepo/" title="组件库框架 - 1 搭建pnpn monorepo">组件库框架 - 1 搭建pnpn monorepo</a><time datetime="2023-10-27T06:36:00.000Z" title="发表于 2023-10-27 14:36:00">2023-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/01/12/other/%E4%BD%BF%E7%94%A8OSS+PicGo%E6%90%AD%E5%BB%BA%E5%9B%BE%E5%BA%8A/" title="使用OSS+PicGo搭建图床"><img src="https://chao-picture-bed.oss-cn-hangzhou.aliyuncs.com/blog/vue/popperJs/202301121557184.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="使用OSS+PicGo搭建图床"/></a><div class="content"><a class="title" href="/2023/01/12/other/%E4%BD%BF%E7%94%A8OSS+PicGo%E6%90%AD%E5%BB%BA%E5%9B%BE%E5%BA%8A/" title="使用OSS+PicGo搭建图床">使用OSS+PicGo搭建图床</a><time datetime="2023-01-12T07:39:00.000Z" title="发表于 2023-01-12 15:39:00">2023-01-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023  <i id="heartbeat" class="fa fas fa-heartbeat"></i> 刘超</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><canvas id="universe"></canvas><script defer src="/js/universe.js"></script><script defer src="https://fastly.jsdelivr.net/npm/hexo-theme-volantis@latest/source/js/issues.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch-lite.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js/dist/instantsearch.production.min.js"></script><script src="/js/search/algolia.js"></script></div></div><!-- hexo injector body_end start --><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>